package com.winning.sx.microframework.dao.sysmanage;

import com.winning.dapi.data.api.IDataAccessService;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.model.exception.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class ConstDao {
    @Autowired
    private IDataAccessService dataAccessService;

    public DataTable queryCount(Map<String, Object> map) throws CustomException {
        DataTable table = null;
        try {
            String sql = "set nocount on;" +
                    "select * from  BASE_Const  where 1=1 and state='200001' " +
                    (map.get("type") != null ? (" and type=" + "'" + map.get("type")) + "'" : "")+
                    " order by code ";
            table = dataAccessService.querySql(sql);
        } catch (Exception e) {
            throw new CustomException(-2001, "获取信息失败！");
        }
        return table;
    }

    public DataTable queryCode(Map<String, Object> map) throws CustomException {
        DataTable table = null;
        try {
            String sql = "set nocount on;" +
                    "select * from  BASE_ConstType  where 1=1 " +
                    (map.get("code") != null ? (" and code like" + "'" + map.get("code")) + "%'" : "");
            table = dataAccessService.querySql(sql);
        } catch (Exception e) {
            throw new CustomException(-2001, "获取信息失败！");
        }
        return table;
    }

    public DataTable query(Map<String, Object> map) throws CustomException {
        DataTable table = null;
        try {
            String sql = "set nocount on;" +
                    "select * from  BASE_Const  where 1=1 " +
                    (map.get("code") != null ? (" and code=" + "'" + map.get("code")) + "'" : "");
            table = dataAccessService.querySql(sql);
        } catch (Exception e) {
            throw new CustomException(-2001, "获取信息失败！");
        }
        return table;
    }

    public DataTable queryConst(Map<String, Object> map) throws CustomException {
        DataTable table = null;
        try {
            String sql = "set nocount on;" +
                    "select * from  BASE_ConstType  where 1=1 " +
                    (map.get("code") != null ? (" and code like" + "'" + map.get("code")) + "%'" : "");
            table = dataAccessService.querySql(sql);
        } catch (Exception e) {
            throw new CustomException(-2001, "获取信息失败！");
        }
        return table;
    }

    public String queryValue(String key) throws CustomException {

        try {
            String sql = "select * from  BASE_Const  where   code=:code ";
            Map param = new HashMap();
            param.put("code", key);
            return dataAccessService.querySqlUsingParam(sql, param).getDataRows().get(0).get("name").toString();
        } catch (Exception e) {
            return key;
        }
    }

    public DataTable queryModel(Map<String, String> map) throws CustomException {

        DataTable table = null;
        try {
            String sql = "select * from BASE_EquipmentDetail where 1=1 and validFlag='100001'" +
                    (map.get("type") == null ? "" : (" and type='" + map.get("type") + "'")) +
                    (map.get("state") == null ? "" : (" and state='" + map.get("state") + "'")) +
                    (map.get("brand") == null ? "" : (" and brand='" + map.get("brand") + "'"));
            table = dataAccessService.querySql(sql);
        } catch (Exception e) {
            throw new CustomException(-2001, "获取信息失败！");
        }
        return table;
    }

    public List queryMyOrg(Map<String, String> map) throws CustomException {
        try {
            String sql =
                    "select * from  BASE_Organization  where id="+map.get("superOrganizationId").toString()+" and validFlag='100001' and organizationType='"+map.get("organizationType").toString()+"'" +
                            (map.get("warehouseFlag") == null ? "" : (" and warehouseFlag='" + map.get("warehouseFlag") + "'"))  ;
            if("0".equals(map.get("superOrganizationId").toString()))sql =
                    sql= "select * from  BASE_Organization  where superOrganizationId="+map.get("superOrganizationId").toString()+" and validFlag='100001' and organizationType='"+map.get("organizationType").toString()+"'" +
                            (map.get("warehouseFlag") == null ? "" : (" and warehouseFlag='" + map.get("warehouseFlag") + "'"))  ;
            List list = dataAccessService.querySql(sql).getDataRows();
            for(int i=0;i<list.size();i++){
                Map bean=(Map)list.get(i);
                bean.put("childrenOrg",queryChildren(bean.get("id").toString(),map.get("warehouseFlag")));
            }
            return list;
        } catch (Exception e) {
            throw new CustomException(-2001, "获取信息失败！");
        }
    }

    private List queryChildren(String superId,Object warehouseFlag) throws Exception {
            String sql =
                    "select * from  BASE_Organization  where validFlag='100001' and superOrganizationId=" + superId+
                            (warehouseFlag == null ? "" : (" and warehouseFlag='" + warehouseFlag.toString() + "'"));
            List list = dataAccessService.querySql(sql).getDataRows();
            for(int i=0;i<list.size();i++){
                Map bean=(Map)list.get(i);
                bean.put("childrenOrg",queryChildren(bean.get("id").toString(),warehouseFlag));
            }
            return list;
    }
}
